#!/bin/bash

# 修复导入问题的脚本

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

PROJECT_DIR=$(dirname "$(readlink -f "$0")")

log_info "修复项目导入问题..."

# 检查项目结构
log_info "检查项目结构..."
if [ ! -d "$PROJECT_DIR/db" ]; then
    log_error "db目录不存在"
    exit 1
fi

if [ ! -d "$PROJECT_DIR/db/models" ]; then
    log_error "db/models目录不存在"
    exit 1
fi

if [ ! -f "$PROJECT_DIR/db/models/file.py" ]; then
    log_error "db/models/file.py文件不存在"
    exit 1
fi

if [ ! -f "$PROJECT_DIR/db/models/base.py" ]; then
    log_error "db/models/base.py文件不存在"
    exit 1
fi

log_success "项目结构检查通过"

# 检查Python路径
log_info "检查Python路径..."
cd "$PROJECT_DIR"

# 激活虚拟环境
if [ -d "yxyweb_venv" ]; then
    source yxyweb_venv/bin/activate
    log_info "已激活虚拟环境"
else
    log_error "虚拟环境不存在，请先运行 ./install.sh"
    exit 1
fi

# 测试导入
log_info "测试模块导入..."
python3 test_imports.py

log_success "修复完成！现在可以尝试启动服务："
echo "  ./start.sh"
